﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Settings;

namespace SMSROUTER.Business
{
    public partial class Operator
    {
        #region Singleton Pattern
        private static object _lock = new object();
        private static List<Operator> _instance;
        public static List<Operator> GetInstance()
        {
            lock (_lock)
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        using (var context = new DBDataContext(AppSetting.ConnectionString))
                        {
                            _instance = context.Operators.ToList();
                        }
                    }
                }
            }
            return _instance;
        }
        public static void ReloadData()
        {
            lock (_lock)
            {
                using (var context = new DBDataContext(AppSetting.ConnectionString))
                {
                    _instance = context.Operators.ToList();
                }
            }
        }
        #endregion Singleton Pattern

        #region Static method

        public static Operator GetByID(int operatorid)
        {
            try
            {
                return GetInstance().Find(p => p.OperatorID == operatorid);
            }
            catch
            {
                return null;
            }
        }

        public static Operator GetByGatewayID(string gateway_id)
        {
            try
            {
                return GetInstance().Find(p => p.GatewayID == gateway_id);
            }
            catch
            {
                return null;
            }
        }

        public static Operator GetByOperatorName(string operator_name)
        {
            try
            {
                return GetInstance().Find(p => p.OperatorName == operator_name);
            }
            catch
            {
                return null;
            }
        }

        public static string GetOperatorGateWay(int? operatorID)
        {
            if (operatorID.HasValue)
            {
                Operator item = GetByID(operatorID.Value);
                return (item != null ? item.GatewayID : "");
            }
            return "";
        }

        public static Operator FindItem(Predicate<Operator> predicate)
        {
            try
            {
                return GetInstance().Find(predicate);
            }
            catch
            {
                return null;
            }
        }

        public static string GetNameByID(DBDataContext context, int operatorID)
        {
            try
            {
                return context.Operators.FirstOrDefault(p => p.OperatorID == operatorID).OperatorName;
            }
            catch
            {
                return "";
            }
        }

        public static List<Operator> Paging(DBDataContext context, int currentPage, int pageSize, ref int totalRecord, string opearatorName, string gatewayId)
        {
            var resutl = context.Operators.OrderBy(p => p.OrderID).AsQueryable();
            if (!string.IsNullOrWhiteSpace(opearatorName))
            {
                resutl = resutl.Where(p => p.OperatorName == opearatorName);
            }
            if (!string.IsNullOrWhiteSpace(gatewayId))
            {
                resutl = resutl.Where(p => p.GatewayID == gatewayId);
            }
            totalRecord = resutl.Count();
            return resutl.Skip(((currentPage - 1) < 0 ? 0 : (currentPage - 1)) * pageSize).Take(pageSize).ToList();
        }
        #endregion Static method
    }
}
